package cn.yunhe.controller;


import cn.yunhe.entity.User;
import cn.yunhe.service.UserService;
import cn.yunhe.util.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;


@Controller
public class UserController {
    @Autowired
    @Qualifier("userService")
    private UserService userService;
    /**
     * 会员中心：用户中心
     */
    @RequestMapping("/intousercenter")
    public ModelAndView selectUserByName(HttpSession session){
        ModelAndView mv = new ModelAndView();
        User user = (User) session.getAttribute("Curr_User");
        User user1 = userService.getUserByName(user);
        mv.addObject("user",user1);
        mv.setViewName("usercenter");
        return  mv;
    }

    /***
     * 用户历史
     * @return
     */
    @RequestMapping("/examhistory")
    public ModelAndView getExamHistory(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("exam-history");
        return mv;
    }

    /**
     * 登陆
     * @return
     * @author
     */
    @RequestMapping("/user/login")
    public String login(User user, HttpSession session){
        User u = userService.login(user);
        if (u!=null){//登陆成功
            session.setAttribute("Curr_User",u);
            return "home";
        }else{

            return "redirect:/static/login.html?123146";
        }
    }
    @RequestMapping("/user/home")
    public ModelAndView ppp(){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("home");
        return mv;
    }
    @RequestMapping("/user/logout")
    public String Logout(){


        return "redirect:/static/login.html";
    }
    @RequestMapping("/add")
    public String addUser(User user, HttpServletRequest request){
        Integer depId = Integer.valueOf(request.getParameter("depId"));
        user.getDepartment().setDepId(depId);
        userService.addUser(user);
        Map map = new HashMap();
        map.put("userId", user.getUserId());
        map.put("roleId", 3);
        userService.addUser2Role(map);
        return "redirect:/static/login.html";
    }
    @RequestMapping("/update")
    public ModelAndView toEdit(HttpSession session){
        ModelAndView mv = new ModelAndView();
        User user = (User) session.getAttribute("Curr_User");
        User user1 = userService.getUserById(user.getUserId());
        mv.addObject("user",user1);
        mv.setViewName("setting");
        return mv;
    }
    @RequestMapping("/toEdit")
    public ModelAndView edit(HttpSession session, User user){
        User u = (User) session.getAttribute("Curr_User");
        user.setUserId(u.getUserId());
        int a = userService.updateUser(user);
        ModelAndView mv = new ModelAndView("setting");
        mv.setView(new MappingJackson2JsonView());
        mv.addObject("msg", a);
        return mv;
     }
        @RequestMapping("change-password")
       public ModelAndView changePwd(HttpSession session){
        ModelAndView mv = new ModelAndView();
        User user = (User) session.getAttribute("Curr_User");
        User user1 = userService.getUserById(user.getUserId());
        mv.addObject("user",user1);
        mv.setViewName("change-password");
        return mv;
    }
    @RequestMapping("/editPwd")
    public ModelAndView editUser(HttpSession session,User user){
        User u = (User) session.getAttribute("Curr_User");
        user.setUserId(u.getUserId());
        int a = userService.updatePassword(user);
        System.out.println(a);
        ModelAndView mv = new ModelAndView("setting");
        mv.setView(new MappingJackson2JsonView());
        mv.addObject("msg", a);
        session.invalidate();
        return mv;
   }
}
